package org.eCon.maven.plugin;
/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.maven.plugin.logging.Log;
import org.eCon.maven.iPlugin.IPropertySet;

import java.io.*;

/**
 * User: eCon
 * Date: 04-Aug-2008
 * Time: 22:12:46
 */
public abstract class AbstractGWTPlugin implements IPropertySet {

    protected Log log;

    protected String gwt_module;

    protected String gwt_os;

    protected String gwt_Home;

    protected String java_Home;

    protected boolean gwt_create_run;

    protected String[] additional_classpathes;

    protected String javac_output;

    protected String currentDir;

    protected String cpDelimiter = ":";
    protected String COMPILER = "com.google.gwt.dev.GWTCompiler";
    protected String SHELL = "com.google.gwt.dev.GWTShell";

    protected String java_path = null;

    protected String gwt_relative_out;

    protected String gwt_relative_src;

    protected String jvm_param;
    

    protected String getClasspath() {

        StringBuilder sb = new StringBuilder(100);

        sb.append(this.gwt_Home).append(File.separator).append(getEnvLib()).append(cpDelimiter);
        sb.append(this.gwt_Home).append(File.separator).append("gwt-user.jar").append(cpDelimiter);
        sb.append(this.currentDir).append(File.separator).
                append(this.gwt_relative_src).append(cpDelimiter);
        if (additional_classpathes != null && additional_classpathes.length > 0) {
            for (int i = 0; i < additional_classpathes.length; i++) {
                sb.append(additional_classpathes[i]).append(cpDelimiter);
            }
        }

        return sb.toString();
    }

    protected abstract String getEnvLib();

    public String createJavaPath() {
        if (java_path == null) {
            if (java_Home != null) {
                StringBuffer sb = new StringBuffer(50);
                sb.append("\"").append(this.java_Home).append(File.separator).append("bin").
                        append(File.separator).append("java").append("\"");

                java_path = sb.toString();
            } else {
                java_path = "java";
            }
        }

        return java_path;
    }

    protected void writeToFile(String filePath, String lines) {
        File file = new File(filePath);
        PrintWriter pw = null;
        try {
            if (!file.exists())
                file.createNewFile();
            pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(filePath)));
            pw.print(lines);
            pw.flush();

        } catch (IOException io) {
            log.error("Error while creating gwt shell script. ", io);
        } finally {
            if (pw != null)
                pw.close();
        }
    }


    public void setLog(Log log) {
        this.log = log;
    }

    public void setAdditional_classpathes(String[] additional_classpathes) {
        this.additional_classpathes = additional_classpathes;
    }


    public void setGwt_os(String gwt_os) {
        this.gwt_os = gwt_os;
    }

    public void setGwt_Home(String gwt_Home) {
        this.gwt_Home = gwt_Home;
    }

    public void setJava_Home(String java_Home) {
        this.java_Home = java_Home;
    }

    public void setCurrentDir(String currentDir) {
        this.currentDir = currentDir;
    }

    public void setGwt_create_run(boolean gwt_create_run) {
        this.gwt_create_run = gwt_create_run;
    }

    public void setJavac_output(String javac_output) {
        this.javac_output = javac_output;
    }


    public void setGwt_module(String gwt_module) {
        this.gwt_module = gwt_module;
    }


    public void setGwt_relative_out(String gwt_relative_out) {
        this.gwt_relative_out = gwt_relative_out;
    }


    public void setGwt_relative_src(String gwt_relative_src) {
        this.gwt_relative_src = gwt_relative_src;
    }

    public void setJvm_param(String jvm_param) {
        this.jvm_param = jvm_param;
    }

    protected String getHtmlName(String module) {
        if (module != null) {
            int index = module.lastIndexOf(".");

            String name = (index != -1) ? module.substring(index + 1) : module;
            return name + ".html";
        }

        return null;
    }

    protected void controlAndAppend(String src, String cmd, StringBuffer sb, boolean isDir) {
        if (src != null && src.trim().length() != 0) {
            if (isDir) {
                src = this.currentDir + File.separator + src;
                File dir = new File(src);
                if (!dir.exists()) {
                    if (!dir.mkdirs()) {
                        throw new RuntimeException("Could not create the directory:" + dir.getAbsolutePath());
                    }
                }
            }

            if (cmd != null) {
                sb.append("-");
                sb.append(cmd);
                sb.append(" ");
            }

            sb.append(src);
            sb.append(" ");
        }
    }
}
